package com.iskny.framework.admin.controllor;

import java.util.List;

import com.iskny.framework.kit.UserMenusKit;
import com.iskny.framework.model.LoginResult;
import com.iskny.framework.model.MenuBean;
import com.iskny.framework.model.User;
import com.iskny.framework.model.UserBean;
import com.iskny.framework.session.UserSession;
import com.iskny.framework.admin.interceptor.LoginInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.core.Controller;

@Before(LoginInterceptor.class)
public class AdminIndexAction extends Controller {

	/**
	 * 后台登录页面
	 */
	@ClearInterceptor
	public void login(){
		createToken();
	}
	
	/**
	 * 验证用户登录
	 */
	@ClearInterceptor
	@Before(AdminLoginValidator.class)
	public void checkuser(){
		System.out.println("admin-checkuser:success");
		User user = new User();
		String loginname = getPara("loginname","");
		String password = getPara("password","");
		LoginResult ret = user.checkUser(loginname, password,user);
		switch (ret) {
		case success:
			UserSession s = new UserSession(getSession());
			Long id = user.getIdByLoginname(loginname);
			s.setAttr(new UserBean(id,loginname,UserBean.ADMIN));
			redirect("/admin");
			break;
		default:
			redirect("/admin/login");
			break;
		}
		System.out.println(loginname);
	}
	/**
	 * 获取当前用户菜单
	 */
	public void menus(){
		UserSession s = new UserSession(getSession());
		List<MenuBean> menus = UserMenusKit.loadMenus(s.getAttr().getId());
		setAttr("menus", menus);
	}
	
	/**
	 * 后台登录后首页
	 */
	public void index(){
		System.out.println("adminindex:success");
		menus();//查询菜单
	}
	
	public void logout(){
		new UserSession(getSession()).clear();
	}
	
}
